package cn.openea.perpartner.meily.web.entity.dto;

import cn.openea.perpartner.meily.web.entity.GroupBuying;
import cn.openea.perpartner.meily.web.entity.ProvideSamplesSub;
import cn.openea.perpartner.meily.web.entity.RevenueRecordsArc;
import cn.openea.perpartner.meily.web.service.IGroupBuyingService;
import cn.openea.perpartner.meily.web.service.IGroupBuyingSubService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.function.Function;

/**
 * @author
 * @version 1.0.1
 * @Description TODO
 * @createTime 2023年03月29日 16:18:00
 */
@Component
@Slf4j
public class GroupBuyingDTOFactory {

    @Autowired
    private IGroupBuyingSubService groupBuyingSubService;

    /**
     * 商品销售DTO
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @SuperBuilder
    public static class GroupBuyingInfoDTO {
        @TableId
        private String id;
        @ApiModelProperty(value = "工单号")
        private String taskNumber;
        @ApiModelProperty(value = "创建时间")
        @TableField(fill = FieldFill.INSERT)
        private String createTime;
        @ApiModelProperty(value = "更新时间")
        @TableField(fill = FieldFill.INSERT_UPDATE)
        private String updateTime;
        @ApiModelProperty(value = "发起人id")
        private String initiatorId;
        @ApiModelProperty(value = "发起人昵称")
        private String initiatorName;
        @ApiModelProperty(value = "标题")
        private String title;
        @ApiModelProperty(value = "描述")
        private String description;
        @ApiModelProperty(value = "单价")
        private String unitPrice;
        @ApiModelProperty(value = "商品总量")
        private String totalQuantity;
        @ApiModelProperty(value = "行业")
        private String industry;
        @ApiModelProperty(value = "关键字")
        private String keyWord;
        @ApiModelProperty(value = "折扣信息(JSONArray字符串)")
        private String discountInfo;
        @ApiModelProperty(value = "开始时间")
        private String startTime;
        @ApiModelProperty(value = "结束时间")
        private String endTime;
        @ApiModelProperty(value = "商品记录唯一ID")
        private String goodsId;
        @ApiModelProperty(value = "商品编号")
        private String goodsNo;
        @ApiModelProperty(value = "商品名称")
        private String goodsName;
        @ApiModelProperty(value = "商品图片")
        private String goodsImages;
        @ApiModelProperty(value = "商品原价")
        private String originalPrice;
        @ApiModelProperty(value = "帮卖报酬")
        private String distributionReward;
        @ApiModelProperty(value = "定制ID")
        private String sceneAssetsId;
        @ApiModelProperty(value = "场景ID")
        private String sceneId;
        @ApiModelProperty(value = "每人参与次数。0:不限；1:仅限一次;(默认0)")
        private String joinNum;
        @ApiModelProperty(value = "剩余的商品数量")
        private String remainQuantity;
        @ApiModelProperty(value = "已团购的商品数量(默认0)")
        private String usedQuantity;
        @ApiModelProperty(value = "参与团购的人数(默认0)")
        private String customerNum;
        @ApiModelProperty(value = "状态。0:废弃；1:启用；2:结束；(默认1)")
        private String status;
        @ApiModelProperty(value = "团购实际结束时间")
        private String finishTime;
        @ApiModelProperty(value = "参与的拍档id")
        private String joinMember;
        @ApiModelProperty(value = "参与的拍档昵称")
        private String joinMemberShow;
        @ApiModelProperty(value = "参与团购的记录id")
        private String allSubDocunid;
        @ApiModelProperty(value = "测评主文档ID")
        private String reviewMainDocunid;
        @ApiModelProperty(value = "测评子文档ID")
        private String reviewSubDocunid;
        @ApiModelProperty(value = "周期串")
        private String cycle;
        @ApiModelProperty(value = "拼团订单详情")
        private JSONObject groupOrdersInfo;

    }

    public Function<GroupBuying, GroupBuyingInfoDTO> groupBuyingToInfoDto = (groupBuying) -> {
        GroupBuyingInfoDTO infoDTO = new GroupBuyingInfoDTO();
        BeanUtils.copyProperties(groupBuying, infoDTO);
        JSONObject obj = groupBuyingSubService.getOrderCompletion(groupBuying, groupBuying.getDistributionReward());
        obj.put("shareTitle", groupBuyingSubService.getShareTitle(groupBuying));// 2023-05-10 add by lys
        infoDTO.setGroupOrdersInfo(obj);
        return infoDTO;
    };

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @SuperBuilder
    public static class GroupBuyingStatisticsDto{
        @ApiModelProperty(value = "代理商品数量")
        @Builder.Default
        private Integer agentGoodsQuantity = 0;
        @ApiModelProperty(value = "代理销售收入")
        @Builder.Default
        private BigDecimal agentSalesRevenue = BigDecimal.ZERO;
    }

}
